package com.ph.shopping.facade.product.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ph.shopping.common.core.base.BaseService;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.page.PageBean;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.mapper.PhProductClassifyExtendMapper;
import com.ph.shopping.facade.mapper.PhProductClassifyMapper;
import com.ph.shopping.facade.mapper.PhProductMapper;
import com.ph.shopping.facade.product.entity.PhProductClassify;
import com.ph.shopping.facade.product.service.IPhProductClassifyService;
import com.ph.shopping.facade.product.vo.PhProductClassifyExtendVo;
import com.ph.shopping.facade.product.vo.PhProductClassifyVo;
import com.ph.shopping.facade.product.vo.PhProductVo;
/**
 * @项目：phshopping-service-product
 * @描述：商品类型
 * @作者： hewl
 * @创建时间：2017年3月6日 上午11:15
 * @Copyright by hewl
 * @version 1.0.0
 */
@Component
@Service(version="1.0.0")
public class PhProductClassifyServiceImpl extends BaseService implements IPhProductClassifyService{
	
	//引用商品类型的mapper
	@Autowired
	private PhProductClassifyMapper phProductClassifyMapper;
	//引用商品类型商城扩展的mapper
	@Autowired
	private PhProductClassifyExtendMapper phProductClassifyExtendMapper;
	//商品的Mapper
	@Autowired
	private PhProductMapper phProductMapper;

	@Override
	public Result addProductClassify(PhProductClassify phProductClassify) throws Exception {
		phProductClassifyMapper.insertSelective(phProductClassify);
		return getResult(ResultEnum.SUCCESS);
	}

	@Override
	public Result updateProductClassify(PhProductClassify phProductClassify) throws Exception {
		phProductClassifyMapper.updateByPrimaryKeySelective(phProductClassify);
		return getResult(ResultEnum.SUCCESS);
	}
	/**
	 * 查看详情 或者多条详情语句(不要使用分页插件查询)
	 * @author hewl
	 * @param phProductClassifyVo
	 * @return List<PhProductClassifyVo>
	 */
	public List<PhProductClassifyVo> getProductClassifyVoDetail(PhProductClassifyVo phProductClassifyVo) throws Exception{
		return phProductClassifyMapper.getProductClassifyVoDetail(phProductClassifyVo);
	}
	/**
	 * 列表数据展示
	 * @author hewl
	 * @param phProductClassifyVo
	 * @return List<PhProductClassifyVo>
	 */
	public List<PhProductClassifyVo> getProductClassifyVoList(PhProductClassifyVo phProductClassifyVo) throws Exception{
		return phProductClassifyMapper.getProductClassifyVoList(phProductClassifyVo);
	}
	
	/**
	 * 列表分页数据展示
	 * @author hewl
	 * @param phProductClassifyVo
	 * @return Result
	 */
	public Result getProductClassifyVoPageList(PageBean pageBean,PhProductClassifyVo phProductClassifyVo) throws Exception{
		PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
		List<PhProductClassifyVo> list = phProductClassifyMapper.getProductClassifyVoList(phProductClassifyVo);
		PageInfo<PhProductClassifyVo>  pageInfo =new PageInfo<PhProductClassifyVo>(list);
		return new Result(ResultEnum.SELECT_PRODUCT_CLASSIFY_EXCEPTION.getCode(),"",pageInfo.getList(),pageInfo.getTotal());
	}
	
	
	/**
	 * 商品分类商城扩展
	 * @author hewl
	 * @param PhProductClassifyExtendVo
	 * @return Map<String,Object>
	 *//*
	public List<PhProductClassifyVo> getPhProductClassifyExtendVoList(PhProductClassifyVo extendVo) throws Exception{
		List<PhProductClassifyVo> list = phProductClassifyMapper.getProductClassifyVoAchildList(extendVo);
		if(list.size()>0){//先判断是否有数据
			for(PhProductClassifyVo vo:list){//循环查找自己的二级
				List<PhProductVo> listProduct=new ArrayList<PhProductVo>();
				PhProductClassifyExtendVo v = new PhProductClassifyExtendVo();
				v.setFjId(String.valueOf(vo.getId()).toString());
				PhProductClassifyVo classifyVo =new PhProductClassifyVo();
				classifyVo.setParentId(vo.getId());
				classifyVo.setDeleteFlag(1);
				List<PhProductClassifyVo> classifyVoList = phProductClassifyMapper.getProductClassifyVoAchildList(classifyVo);
				if(classifyVoList.size()>0){
					vo.setPhProductClassifyVoList(classifyVoList);
				}
				//二级、三级
				List<PhProductClassifyExtendVo> listExtendVo = phProductClassifyExtendMapper.getPhProductClassifyExtendVoList(v);
				if(listExtendVo.size()>0){
					for(PhProductClassifyExtendVo vBean:listExtendVo){//循环三级
						if(StringUtils.isNotEmpty(vBean.getZjid())){//先判断三级id是否为空
							PhProductVo phProductVo = new PhProductVo();//初始化商品信息
							phProductVo.setAuditState(3);
							phProductVo.setDeleteFlag(1);
							phProductVo.setSaleNoSaleStatus(1);
							phProductVo.setProductClassifyId(Long.valueOf(vBean.getZjid()).longValue());//装进类别id
							List<PhProductVo> productList = phProductMapper.getPhProductVoDeliList(phProductVo);//查询商品信息
							if(productList.size()>0)//是否存在商品
								listProduct.addAll(productList);
						}
					}
				}
				vo.setPhProductVoList(listProduct);//装载商品信息
			}
		}
		return list;
	}*/
	
	
	
	
	/**
	 * 商城首页页面单独模块
	 * @author hewl
	 * @param PhProductClassifyVo
	 * @return  List<PhProductClassifyVo> 
	 */
	public List<PhProductClassifyVo> getProductIndexList(PhProductClassifyVo extendVo) throws Exception{
		List<PhProductClassifyVo> list = phProductClassifyMapper.getProductClassifyVoAchildList(extendVo);
		if(list.size()>0){//先判断是否有数据
			for(PhProductClassifyVo vo:list){//循环查找自己的二级
				PhProductClassifyVo classifyVo =new PhProductClassifyVo();
				List<PhProductVo> listProduct=new ArrayList<PhProductVo>();
				classifyVo.setParentId(vo.getId());
				classifyVo.setDeleteFlag(1);
				List<PhProductClassifyVo> listNext = phProductClassifyMapper.getProductIndexList(classifyVo);
				if(listNext.size()>0){
					for(PhProductClassifyVo classVo : listNext){
						listProduct.addAll(classVo.getPhProductVoList());
					}
				}
				vo.setPhProductVoList(listProduct);
				vo.setPhProductClassifyVoList(listNext);
			}
		}
		return list;
	}
	/**
	 * 单条数据展示
	 * @author hewl
	 * @param phProductClassifyVo
	 * @return PhProductClassifyVo
	 */
	public PhProductClassifyVo getProductClassifyVo(PhProductClassifyVo phProductClassifyVo) throws Exception{
		List<PhProductClassifyVo>  list = phProductClassifyMapper.getProductClassifyVoList(phProductClassifyVo);
		if(list.size()>0){
			phProductClassifyVo = list.get(0);
		}
		return phProductClassifyVo;
	}
	/**
	 * 子级查询
	 * @param phProductClassifyVo
	 * @return Result
	 */
	public List<PhProductClassifyVo> getProductClassifyVoAchildList(PhProductClassifyVo phProductClassifyVo) throws Exception{
		 return phProductClassifyMapper.getProductClassifyVoAchildList(phProductClassifyVo);
	}
	/**
	 * 逻辑删除商品类型
	 * @author hewl
	 * @param phProductClassifyVo:id
	 * @return Result
	 */
	@Transactional(rollbackFor=Exception.class)
	public Result deleteLogicPhProductClassify(PhProductClassifyVo phProductClassifyVo) throws Exception{
		Result result=new Result();
		PhProductClassifyExtendVo classifyExtendVo = new PhProductClassifyExtendVo();//实例化类别扩展表
		classifyExtendVo.setFjId(String.valueOf(phProductClassifyVo.getId()));
		List<PhProductClassifyExtendVo> classifyExtendVoList = phProductClassifyExtendMapper.getPhProductClassifyExtendVoByList(classifyExtendVo);//查询分类一、二、三级id
		if(classifyExtendVoList.size()>0){//判断是否存在二级、一级
			String classById = "";
			for(PhProductClassifyExtendVo v:classifyExtendVoList){
				if(StringUtils.isNotEmpty(v.getZhid())){
					if(phProductClassifyVo.getClassifyLevel()==2){
						classById+=v.getZhid().substring(v.getZhid().indexOf(",")+1, v.getZhid().length())+",";
					}else{
						classById+=v.getZhid()+",";
					}
						
				}
			}
			//判断是否有类别id
			if(StringUtils.isNotEmpty(classById)){
					phProductClassifyVo.setIdExtend(Arrays.asList(classById.substring(0, classById.lastIndexOf(",")).split(",")));
			}
		}
		//执行删除
		Integer returnValue = phProductClassifyMapper.deleteLogicPhProductClassify(phProductClassifyVo);
		result.setCode("200");
		if(returnValue>0){
			result.setMessage("删除成功");
		}
		return result;
	}

	/**
	 * 根据一级分类查下面所有被引用的商品数量
	 */
	@Override
	public Integer getExistProductByClassifyLevelOne(PhProductClassifyVo phProductClassifyVo) throws Exception {
		return phProductClassifyMapper.getExistProductByClassifyLevelOne(phProductClassifyVo);
	}

	/**
	 * 根据二级分类查下面所有被引用的商品数量
	 */
	@Override
	public Integer getExistProductByClassifyLevelTwo(PhProductClassifyVo phProductClassifyVo) throws Exception {
		return phProductClassifyMapper.getExistProductByClassifyLevelTwo(phProductClassifyVo);
	}
}
